class Solution {
    public int trap(int[] height) {
        if(height.length<=2)return 0;
            int[] maxLeft= new int[height.length];
            int[] maxRight=new int[height.length];
            maxLeft[0]=height[0];
        // 记录每个柱子左边柱子最大高度
            for(int i=1;i<height.length;i++){
                maxLeft[i]=Math.max(maxLeft[i-1],height[i]);
            }
            //记录每个柱子右边柱子最大高度
            maxRight[height.length-1]=height[height.length-1];
            for(int i=height.length-2;i>=0;i--){
                maxRight[i]=Math.max(maxRight[i+1],height[i]);
            }
            //求和
            int sum=0;
            for(int i=0;i<height.length;i++){
                int count=Math.min(maxLeft[i],maxRight[i])-height[i];
                sum+=count;
            }
            return sum;
    }
}